import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from "@/utils/token";
import store from "@/store";
import { Message } from "element-ui";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "",
    name: "Index",
    component: () => import("@/views/Index"),
    children: [
      {
        path: "/",
        name: "welcome",
        // meta: {
        //   requireAuth: true
        // },
        component: () => import("@/views/Welcome"),
      },
      {
        path: "/category",
        name: "category",
        component: () => import("@/views/Category"),
      },
      {
        path: "/tag",
        name: "tag",
        component: () => import("@/views/Tag"),
      },
      {
        path: "/archives/:year?/:month?",
        component: (r) =>
          require.ensure([], () => r(require("@/views/Archives")), "archives"),
      },
      {
        path: "/category/:id",
        name: "isCategory",
        component: () => import("@/views/CategoryTag"),
      },
      {
        path: "/tag/:id",
        name: "isTag",
        component: () => import("@/views/CategoryTag"),
      },
      {
        path: "/article/view/:id",
        component: () => import("@/views/BlogView"),
      },
      {
        path: "/userInfo",
        component: () => import("@/views/MyInfo"),
      },
      {
        path: "/log",
        component: () => import("@/views/Log"),
      },
    ],
  },
  {
    path: "/login",
    component: (r) =>
      require.ensure([], () => r(require("@/views/Login")), "login"),
  },
  {
    path: "/register",
    component: (r) =>
      require.ensure([], () => r(require("@/views/Register")), "register"),
  },

  {
    path: "/article/write/:id?",
    component: (r) =>
      require.ensure([], () => r(require("@/views/BlogWrite")), "blogwrite"),
    meta: {
      requireLogin: true,
    },
  },
  {
    path: "/manage",
    name: "manage",
    component: () => import("@/views/manage/Index"),
    redirect: "/manage/welcome",
    children: [
      {
        path: "/manage/welcome",
        name: "manageWelcome",
        meta: {
          requireLogin: true,
        },
        component: () => import("@/views/manage/Welcome"),
      },
      {
        path: "/manage/user",
        component: () => import("@/views/manage/User"),
      },
      {
        path: "/manage/article",
        component: () => import("@/views/manage/Article"),
      },
      {
        path: "/manage/category",
        component: () => import("@/views/manage/Category"),
      },
      {
        path: "/manage/tag",
        component: () => import("@/views/manage/Tag"),
      },
      {
        path: "/manage/doc",
        component: () => import("@/views/manage/Doc"),
      },
      {
        path: "/manage/log",
        component: () => import("@/views/manage/Log"),
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  if (getToken()) {
    // 有token
    if (to.path === "/login") {
      next({ path: "/" });
    }
    // else if (to.path == "/manage/welcome") {
    //   if (store.state.admin != 1) {
    //     next({ path: "/" });
    //   } else {
    //     next();
    //   }
    // }
    else {
      if (store.state.account.length === 0) {
        store
          .dispatch("getUserInfo")
          .then((resp) => {
            if (resp.code != 403) {
              next();
            }
          })
          .catch(() => {
            console.log("error");
            window.localStorage.removeItem("token");
            next();
          });
      } else {
        next();
      }
    }
  } else {
    // 无token
    if (to.matched.some((r) => r.meta.requireLogin)) {
      Message({
        type: "warning",
        showClose: true,
        message: "请先登录哦",
      });
      next({ path: "/" });
    } else {
      next();
    }
  }
});

export default router;
